
#include "Debug.h"
#include <sstream>
#include <string>
#include <stdlib.h>
#include <sys/stat.h>
#include <libpq-fe.h>
#include <map>
#include <string.h>
#include <glib.h>
#include <mqPool.h>
#include "config.h"
#include "define.h"

using namespace std;

extern "C" {
  bool snsConsInit(char *consumer, const char* conf);
  bool snsConsFriends(char *user, GHashTable *frs);
  void snsConsShutdown();
  void snsConsFlush(char *consumer);
};


bool snsConsInit(char *consumer, const char* conf)
{
        struct stat st;
        if (lstat(conf, &st) != 0)
        {
                ERRO<<":"<<conf<<" is not exist!"<<endl;
                return false;
        }

	MqPool *pA =   MqPool::Instance(conf);
	if(pA == NULL)
	{
		ERRO <<" MqPool Instance error" << endl;
		return false;
	}

        return true;
}

bool snsConsFriends(char * user,   GHashTable *frs)
{
	
	if(user == NULL)
		return false;

	MqPool *pAn =  MqPool::Instance();
	if(pAn == NULL)
	{
		ERRO <<" MqPool Instance error" << endl;
		return false;
	}
	MYSQL  *conn = pAn->GetReader(0);
	if(conn == NULL)
	{
		pAn->CloseReader(0, conn);
		cerr<<"can't get mysql reader connection!"<<endl;
		return false;
	}

	int u = atoi(user);
	stringstream sql;
	sql <<"select nick from user_profile where id="<<u;

	if(mysql_real_query(conn, sql.str().c_str(), sql.str().length()) != 0){
		ERRO << mysql_error(conn) << endl;
		pAn->CloseReader(0, conn);
		return  false;
	}
	MYSQL_RES *result;
	MYSQL_ROW row;

	result = mysql_store_result(conn);
	if(result == NULL){
		ERRO << mysql_error(conn) << endl;
		pAn->CloseReader(0, conn);
		return  false;
	}

	long rownum = mysql_num_rows(result);
	if (rownum == 0 )
	{
		mysql_free_result(result);
		pAn->CloseReader(0, conn);
		return false;
	}

	row = mysql_fetch_row(result);
	FObject *uself = new FObject(user, 1,  (char *)row[0]);
	g_hash_table_insert(frs, g_strdup(user), uself);
        mysql_free_result(result);

	sql.str(""); 
	sql<<"select u.partnerid, p.nick from user_relation as u, user_profile as p where ownerid ="<<u<<" and class=1 and u.partnerid=p.id";
	DEBUG1(sql.str());

	if(mysql_real_query(conn, sql.str().c_str(), sql.str().length()) != 0){
		ERRO << mysql_error(conn) << endl;
		pAn->CloseReader(0, conn);
		return  false;
	}
	result = mysql_store_result(conn);
	if(result == NULL){
		ERRO << mysql_error(conn) << endl;
		pAn->CloseReader(0, conn);
		return  false;
	}

	rownum = mysql_num_rows(result);
	if (rownum == 0 )
	{
		mysql_free_result(result);
		pAn->CloseReader(0, conn);
		return true;
	}

	while((row = mysql_fetch_row(result)))
	{
		FObject *uself = new FObject((char *)row[0], 1,  (char *)row[1]);
		g_hash_table_insert(frs, g_strdup((char *)row[0]), uself);
	}
	mysql_free_result(result);
	pAn->CloseReader(0, conn);
	return true;
}

void snsConsShutdown()
{
	MqPool *p =  MqPool::Instance();
	if(p!= NULL)
		MqPool::Release();
}

void snsConsFlush(char * consumer)
{
  // If this consumer keeps statistics or writes to a tx log, then
  // flush the statistics here, otherwise do nothing.
}


